package br.com.fiap.trabalho.test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Scanner;

import br.com.fiap.trabalho.dao.AbstractDAOFactory;
import br.com.fiap.trabalho.dao.ActorDAO;
import br.com.fiap.trabalho.dao.JDBCDAOFactory;
import br.com.fiap.trabalho.entity.Actor;
import br.com.fiap.trabalho.entity.Movie;
import br.com.fiap.trabalho.util.JDBCAuxiliarDAO;

public class ActorTestJDBC {
	
	static int numeroDeTeste;
	static JDBCAuxiliarDAO JDBCUtil =  new JDBCAuxiliarDAO();
	
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
	    System.out.println("Digite a quantidade de registros para testar: ");
	    numeroDeTeste = s.nextInt();
		
		System.out.println("Persistencia com JDBC - Teste entidade Ator");
		System.out.println();
		insertActor();
		listActorByName();
		listActorByAge();
		listActorByMovie();
		removeActor();
		System.out.println("Fim da execucao do teste Ator JDBC...");
	}
	
	private static void insertActor() {
		AbstractDAOFactory factoryJDBCDAO = new JDBCDAOFactory();
		ActorDAO actorJDBCDAO = factoryJDBCDAO.createActorDao();
		long start = System.currentTimeMillis();
		long finish = System.currentTimeMillis();
		System.out.println("-->Inserindo " + numeroDeTeste);
		start = System.currentTimeMillis();
		for (int i = 0; i <= numeroDeTeste; i++) {
			Actor actor = new Actor();
			actor.setFullName("Ator " + i);
			SimpleDateFormat formatador = new SimpleDateFormat("yyyy-MM-dd");
			try {
				actor.setBirthDate(formatador.parse("2000-01-01" + (i+10)));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			actorJDBCDAO.createActor(actor);
		}
        finish = System.currentTimeMillis();
        System.out.println("Total de atores inseridos: " + JDBCUtil.selecionaMaxActor());
        System.out.println(">>Tempo em milissegundos: "+(finish - start));
        System.out.println();
	}
	
	private static void listActorByName(){
		AbstractDAOFactory factoryJDBCDAO = new JDBCDAOFactory();
		ActorDAO actorJDBCDAO = factoryJDBCDAO.createActorDao();
		long start = System.currentTimeMillis();
		long finish = System.currentTimeMillis();
		System.out.println("-->Lista de ator por nome.");
		start = System.currentTimeMillis();
		for (int i = 1; i <= numeroDeTeste; i++) {
			List<Actor> list1SelectActorByName = actorJDBCDAO.selectActorByName("Ator " + i);
		}
		finish = System.currentTimeMillis();
        System.out.println(">>Tempo em milissegundos: "+(finish - start));
        System.out.println();
	}
	
	private static void listActorByAge(){
		AbstractDAOFactory factoryJDBCDAO = new JDBCDAOFactory();
		ActorDAO actorJDBCDAO = factoryJDBCDAO.createActorDao();
		long start = System.currentTimeMillis();
		long finish = System.currentTimeMillis();
		System.out.println("-->Lista de ator por idade.");
		start = System.currentTimeMillis();
		for (int i = 1; i <= numeroDeTeste; i++) {
			List<Actor> list1SelectActorByAge = actorJDBCDAO.selectActorByAge(i);
		}
		finish = System.currentTimeMillis();
        System.out.println(">>Tempo em milissegundos: "+(finish - start));
		System.out.println();
	}
	
	private static void removeActor(){
		AbstractDAOFactory factoryJDBCDAO = new JDBCDAOFactory();
		ActorDAO actorJDBCDAO = factoryJDBCDAO.createActorDao();
		long start = System.currentTimeMillis();
		long finish = System.currentTimeMillis();
		System.out.println("-->Apagando " + numeroDeTeste + " atores.");
		int amountOfActor = JDBCUtil.selecionaMaxActor();
		start = System.currentTimeMillis();
		for (int i = amountOfActor - numeroDeTeste; i <= amountOfActor; i++) {
			Actor actor = new Actor();
			actor.setId(i);
			actorJDBCDAO.deleteActor(actor);
		}
		finish = System.currentTimeMillis();
        System.out.println(">>Tempo em milissegundos: "+(finish - start));
        System.out.println();
	}
	
	private static void listActorByMovie() {
		AbstractDAOFactory factoryJDBCDAO = new JDBCDAOFactory();
		ActorDAO actorJDBCDAO = factoryJDBCDAO.createActorDao();
		long start = System.currentTimeMillis();
		long finish = System.currentTimeMillis();
		System.out.println("-->Lista de ator por Movie.");
		start = System.currentTimeMillis();
		for (int i = 1; i <= numeroDeTeste; i++) {
			Movie movie = new Movie();
			movie.setId(i);
			List<Actor> list1SelectActorByMovie = actorJDBCDAO.selectActorByMovie(movie);
		}
		finish = System.currentTimeMillis();
        System.out.println(">>Tempo em milissegundos: "+(finish - start));
		System.out.println();
	}
}
